home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 3.3 KB | 120 lines | [TEXT/GEOL] |
- Item 8117919 3-July-89 14:51
-
- From: CREMER.M Cremer, Mike
-
- To: KEMINK1 Kemink, Joost
-
- cc: MACAPP.TECH$ MACAPP Tech
-
- Sub: Re: Displaying Error Messages
-
- Joost And Geoff,
-
- Below is my fast-and-dirty solution to personalized MacApp error messages.
- Please note that this is probably neither the official nor sanctioned way to
- handle error messages, i.e. do not be surprised if somebody flames me for being
- so incompetent...
-
- In general, the simple way out is to override TApplication.ShowError like this:
-
- { begin code }
-
- PROCEDURE TMyApplication.ShowError(error: OSErr; message: Longint); OVERRIDE;
-
- CONST
- kMsgCmdErr = msgCmdErr DIV $10000; { needed to check to make sure }
- kMsgAlert = msgAlert DIV $10000; { the error message was generated }
- kMsgLookup = msgLookup DIV $10000; { by us and not MacApp }
-
- TYPE
- { ErrOverlay gives easy access to hi and lo words of the error message }
- ErrOverlay = RECORD
- CASE Integer OF
- 1: (highMsg : Integer;
- lowMsg : Integer);
- 2: (message : Longint);
- END; {ErrOverlay}
-
- VAR
- msg : ErrOverlay;
- cmdStr : Str255;
- msgStr : Str255;
- errStr : Str255;
-
- BEGIN {ShowError}
-
- msg := ErrOverlay(message);
-
- CASE msg.highMsg OF
-
- { pass MacApp errors on to MacApp }
- kMsgCmdErr : INHERITED ShowError(error, message);
- kMsgAlert : INHERITED ShowError(error, message);
- kMsgLookup : INHERITED ShowError(error, message);
-
- { handle our own errors }
- Otherwise
- BEGIN
- { this replaces the command and message strings, as well as }
- { displaying the Mac OSErr number. Change as needed to suit }
- { your tastes. }
- GetIndString(cmdStr, msg.highMsg, msg.lowMsg);
-
- { see below about this next line }
- GetIndString(msgStr, kMyMessageStringList, ErrToStrID(error));
-
- NumToString(error, errStr);
- errStr := Concat('(', errStr, ')');
-
- ParamText(msgStr, errStr, cmdStr, '');
-
- StdAlert(phGenError);
- END;
- END; {CASE highMsg}
- END; {ShowError}
-
- { end code }
-
- Then, when using CatchFailures, you need something like this:
-
- { begin code }
-
- PROCEDURE TMyObject.ErrorProneMethod(parms);
-
- VAR
- fi : FailInfo;
-
- PROCEDURE HandleAnError(error: OSErr; message: Longint);
- VAR
- newMsg : ErrOverlay;
-
- BEGIN {HandleAnError}
- newMsg.highMsg := kMyErrorStringList; { STR# resource ID }
- newMsg.lowMsg := kMyErrorMessage; { index into string list }
- FailNewMessage(error, message, newMsg.message);
- { other cleanups as necessary }
- END; {HandleAnError}
-
- BEGIN {ErrorProneMethod}
- ...
- CatchFailures(fi, HandleAnError);
- Foo(bar);
- Success(fi);
- ...
- END; {ErrorProneMethod}
-
- { end code }
-
- Note that ErrToStrID above is some function that translates a Mac OSErr into
- some kind of resonable string id#. I use a big CASE statement to map OSerr's
- to an index in a single STR# resource (kMyMessageStringList).
-
- This code will allow you to define your own error codes, and trap them at
- ShowAlert, but leaves the MacApp error handlers intact. Anyway, it works for
- me.
-
- Hope this helps.
-
- $mike cremer
-
-